package com.fanshuai.list;

import java.util.Iterator;

public class ArrayList<E> implements List<E> {
    private int size;
    private int capacity;
    private E[] array;

    static int DEFAULT_CAPACITY = 10;
    static int LIMIT = 4 * 1024 * 1024;

    public ArrayList() {
        array = (E[]) new Object[DEFAULT_CAPACITY];
        size = 0;
        capacity = DEFAULT_CAPACITY;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public int capacity() {
        return capacity;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    private void scale() {
        int newCapacity = calcCapacity(capacity);

        E[] newArray = (E[]) new Object[newCapacity];
        System.arraycopy(array, 0, newArray, 0, size);

        array = newArray;
        capacity = newCapacity;
    }

    private int calcCapacity(int capacity) {
        if (2 * capacity < LIMIT) {
            return 2 * capacity;
        } else {
            return capacity + LIMIT;
        }
    }

    @Override
    public void add(E e) {
        if (size + 1 > capacity) {
            scale();
        }

        array[size] = e;
        size++;
    }

    @Override
    public void add(int index, E e) {
        if (index <= 0 || index > size) {
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException, index=" + index);
        }

        if (size + 1 > capacity) {
            scale();
        }

        for (int i = size; i > index; i--) {
            array[i] = array[i - 1];
        }

        array[index] = e;
        size++;
    }

    @Override
    public int indexOf(E e) {
        int index = -1;

        for (int i = 0; i < size; i++) {
            if (array[i].equals(e)) {
                return i;
            }
        }

        return index;
    }

    @Override
    public void remove(E e) {
        int index = indexOf(e);
        if (-1 == index) {
            return;
        }

        for (int i = index; i < size - 1; i++) {
            array[i] = array[i + 1];
        }

        array[size - 1] = null;
        size--;
    }

    @Override
    public void removeByIndex(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("数组越界，index=" + index);
        }

        for (int i = index; i < size - 1; i++) {
            array[i] = array[i + 1];
        }

        array[size - 1] = null;
        size--;
    }

    @Override
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("数组越界，index=" + index);
        }

        return array[index];
    }

    @Override
    public void addAll(List<E> list) {
        for (E e : list) {
            add(e);
        }
    }

    @Override
    public void clear() {
        array = (E[]) new Object[DEFAULT_CAPACITY];
        size = 0;
        capacity = DEFAULT_CAPACITY;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int index = 0;

            @Override
            public boolean hasNext() {
                return index < size;
            }

            @Override
            public E next() {
                return array[index++];
            }
        };
    }

    @Override
    public String toString() {
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                b.append(array[i]);
            } else {
                b.append(array[i]).append(", ");
            }
        }
        return "ArrayList[" + b.toString() + "]";
    }
}
